package com.leyou.service.impl;

import com.leyou.entity.StockEntity;
import com.leyou.service.StockService;
import com.leyou.vo.SkuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



import com.leyou.dao.SkuDao;
import com.leyou.entity.SkuEntity;
import com.leyou.service.SkuService;


@Service("skuService")
public class SkuServiceImpl extends ServiceImpl<SkuDao, SkuEntity> implements SkuService {


    @Autowired
    StockService stockService;

    @Override
    public Boolean deleteSku(Long id) {
        boolean flag = this.remove(new QueryWrapper<SkuEntity>().eq("spu_id", id));
        return flag;
    }

    @Override
    public List<SkuVo> getInfoWithStockBySpuId(Long sid) {
        List<SkuEntity> skus = this.list(new QueryWrapper<SkuEntity>().eq("spu_id", sid));
        List<SkuVo> collect = skus.stream().map(item -> {
            SkuVo skuvo = new SkuVo();
            StockEntity stock = stockService.getById(item.getId());
            skuvo.setStock(stock.getStock());
            BeanUtils.copyProperties(item, skuvo);
            return skuvo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 根据spuid查询sku信息
     * @param sid
     * @return
     */
    @Override
    public List<SkuEntity> getInfoBySpuId(Long sid) {
        return this.list(new QueryWrapper<SkuEntity>().eq("spu_id",sid));
    }
}